<template>
  <div class="dialog">
    <el-dialog :title="'批次' + titleText" :visible.sync="showDialog" width="45%" top="0" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" label-position="left" :model="formData" :rules="rules" class="el-form">
        <el-form-item label="批次编号:" prop="mnecode" class="w50p">
          <el-input v-model="formData.mnecode" clearable />
        </el-form-item>
        <el-form-item label="养殖类型:" prop="breedTypeNo" class="w50p">
          <el-select filterable clearable v-model="formData.breedTypeNo">
            <el-option v-for="(item, index) in breedTypeNoList" :key="index + 'breedTypeNo'" :label="item.breedTypeName"
              :value="item.breedTypeNo" />
          </el-select>
        </el-form-item>
        <el-form-item label="养殖鱼塘:" prop="fishPondId" class="w50p">
          <el-select filterable clearable v-model="formData.fishPondId">
            <el-option v-for="(item, index) in fishPondList" :key="index" :label="item.pondName"
              :value="item.fishPondId" />
          </el-select>
        </el-form-item>
        <el-form-item label="养殖品种:" prop="fishVarietyId" class="w50p">
          <el-select filterable clearable v-model="formData.fishVarietyId">
            <el-option v-for="(item, index) in FishVarietyData" :key="index" :label="item.varietyName"
              :value="item.fishVarietyId" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="养殖方式:" prop="breedType" class="w50p">
          <el-select
            v-model="formData.breedType"
            filterable
            clearable
          >
            <el-option
              v-for="(item, index) in breedTypeList"
              :key="index"
              :label="item.typeName"
              :value="item.typeCode"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="清塘同步完成批次:" prop="clearCompleteFlag" class="w50p">
          <el-select filterable clearable v-model="formData.clearCompleteFlag">
            <el-option v-for="(item, index) in clearCompleteFlag" :key="index" :label="item.typeName"
              :value="item.typeCode" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import FishVarietyApi from "@/api/basic-info-manage/fishVariety"
import FishPondApi from "@/api/basic-info-manage/fishPond"
import BatchMangeApi from "@/api/batch-mange"
import BreedingTypeManageApi from "@/api/basic-info-manage/breeding-type-manage/index";

export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data() {
    let checkInt = (rule, value, callback) => {
      if (value) {
        if (Number(value) && value % 1 === 0) {
          callback();
        } else {
          return callback(new Error("请输入整数！"));
        }
      }
    };
    return {
      rules: {
        storeHouseName: [
          { required: true, message: "请输入批次编号", trigger: ["change", "blur"] }
        ],
        area: [
          { required: true, message: "请输入批次面积", trigger: ["change", "blur"] }, {
            validator: checkInt, trigger: ["change", "blur"]
          }
        ],
      },
      formData: {},
      breedTypeList: [
        {
          typeName: "吊水养殖",
          typeCode: "HANGING_BREED",
        },
        {
          typeName: "传统养殖",
          typeCode: "TRADITIONAL_BREED",
        },
      ],//养殖类型
      clearCompleteFlag: [
        {
          typeName: "是",
          typeCode: true,
        },
        {
          typeName: "否",
          typeCode: false,
        },
      ],//是否选项
      breedTypeNoList: [],
      fishPondList: [],//鱼塘
      FishVarietyData: [],//品种
      showDialog: this.show
    };
  },
  computed: {
  },
  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.formData = {}
      }
      if (v === 'edit') {
        this.formData = this.editObject
        console.log(this.formData);
      }
    }
  },
  created() {
  },
  mounted() {
    this.queryCondition()
    this.queryFishVariety()
    this.queryFishPond()
  },
  methods: {
    queryCondition() {
      BreedingTypeManageApi.queryByCondition().then(res => {
        if (res.success === true) {
          this.breedTypeNoList = res.data || [];
        }
      });
    },
    //查询可选鱼类品种
    queryFishVariety() {
      let params = {
        page: 1,
        size: 1000,
      }
      FishVarietyApi.queryPageByCondition(params).then(res => {
        // console.log('---品种查询---',res);
        this.FishVarietyData = res.data.content;
      })
    },
    //查询可选鱼塘
    queryFishPond() {
      let params = {
        page: 1,
        size: 1000,
      }
      FishPondApi.queryPageByCondition(params).then(res => {
        // console.log('---鱼塘查询---', res);
        this.fishPondList = res.data.content;
      })
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            BatchMangeApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            BatchMangeApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped></style>
